package com.eshop.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.context.ApplicationContextAware;

@Component
public class SpringContextUtils implements ApplicationContextAware
{
    public static ApplicationContext applicationContext;
    
    public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException {
        SpringContextUtils.applicationContext = applicationContext;
    }
    
    public static Object getBean(final String name) {
        return SpringContextUtils.applicationContext.getBean(name);
    }
    
    public static <T> T getBean(final String name, final Class<T> requiredType) {
        return (T)SpringContextUtils.applicationContext.getBean(name, (Class)requiredType);
    }
    
    public static boolean containsBean(final String name) {
        return SpringContextUtils.applicationContext.containsBean(name);
    }
    
    public static boolean isSingleton(final String name) {
        return SpringContextUtils.applicationContext.isSingleton(name);
    }
    
    public static Class<?> getType(final String name) {
        return (Class<?>)SpringContextUtils.applicationContext.getType(name);
    }
}
